package io.kimmking.rpcfx.demo.provider;

import io.kimmking.rpcfx.api.RpcfxResolver;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class DemoResolver implements RpcfxResolver, ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public <T> T resolve(String serviceClass) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class<?> aClass = Class.forName(serviceClass);
        if(aClass.isInterface()){
            String[] pkgAndCls = serviceClass.split("\\.");
            pkgAndCls[pkgAndCls.length-1] = pkgAndCls[pkgAndCls.length-1]+"Impl";
            Class<?> impClass = Class.forName(String.join(".", pkgAndCls));
            return (T)impClass.newInstance();
        }
        return (T)aClass.newInstance();
//        return this.applicationContext.getBean(serviceClass);
    }
}
